﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using ReflectionStudio.Core.Events;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace ReflectionStudio.Core
{ 
    public partial class ThisAddIn
    {
        Outlook.Inspectors inspectors;
        string processtype = "OUTLOOK"; 
        public Outlook.Application OutlookApp
        {
            get
            {
                return outlookApp;
            }
        }
        Outlook.Application outlookApp = null;
 
        Hashtable myHashtable = new Hashtable();
        #region=== setup======
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            List<string> attachments = new List<string>();
            attachments.Add("c:\\somefile.txt");
 
            List<string> recipients = new List<string>();
            recipients.Add("john@contoso.com");
            recipients.Add("john@example.com");
            SendEmailWithAttachments("Test", "Body", recipients, "john@example.com", 
              attachments);
        }
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        } 
      
        public void Close()
        {
            KillProcess(processtype);
        }
          
        private void OpenApp()
        {
            outlookApp = new Outlook.Application();
        }
        private void CheckProcesses(string processtype)
        {
            Process[] AllProcesses = Process.GetProcessesByName(processtype);
            myHashtable = new Hashtable();
            int iCount = 0;

            foreach (Process process in AllProcesses)
            {
                myHashtable.Add(process.Id, iCount);
                iCount = iCount + 1;
            }
        }
        public void KillProcess(string processtype)
        {
            Process[] AllProcesses = Process.GetProcessesByName(processtype);

            // check to kill the right process
            foreach (Process process in AllProcesses)
            {
                if (myHashtable.ContainsKey(process.Id) == false)
                    process.Kill();
            }
            AllProcesses = null;
        }
        #endregion
        #region=== funciton======
        #endregion  
        public void SendEmailWithAttachments( string subject, string body, List<string> recipients, 
            string smtpAddress, List<string> attachments)
        {
 
            // Create a new MailItem and set the To, Subject, and Body properties.
            Outlook.MailItem mail =
                   this.outlookApp.CreateItem
                   (Outlook.OlItemType.olMailItem)
                   as Outlook.MailItem;

            // Set up all the recipients.
            foreach (var recipient in recipients)

            {
                mail.Recipients.Add(recipient);
            }
            if (mail.Recipients.ResolveAll())
            {
                mail.Subject = subject;
                mail.Body = body;
                foreach (string attachment in attachments)
                {
                    mail.Attachments.Add(attachment, Outlook.OlAttachmentType.olByValue);
                }
            } 
            // Retrieve the account that has the specific SMTP address.
            Outlook.Account account = GetAccountForEmailAddress(outlookApp, smtpAddress);
            // Use this account to send the e-mail.
            mail.SendUsingAccount = account;
            mail.Send();
        }
 
        private Outlook.Account GetAccountForEmailAddress(Outlook.Application application, 
            string smtpAddress)
        {
 
            // Loop over the Accounts collection of the current Outlook session.
            Outlook.Accounts accounts = application.Session.Accounts;
            foreach (Outlook.Account account in accounts)
            {
                // When the email address matches, return the account.
                if (account.SmtpAddress == smtpAddress)
                {
                    return account;
                }
            }
            // If you get here, no matching account was found.
            throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", 
                smtpAddress));
        }
        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            //this.Startup += new System.EventHandler(ThisAddIn_Startup);
            //this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        } 
        #endregion 
    }
}
